package cbbx_sm.utils.view;

import java.awt.Container;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.Vector;

import javax.swing.JFrame;

import cbbx_sm.parser.BoundingBox;
import cbbx_sm.parser.CameraData;
import cbbx_sm.parser.Entity;
import cbbx_sm.parser.Frame;
import cbbx_sm.probabilistic_model.Cluster;
import cbbx_sm.utils.ExperimentManager;

public class Viewer extends JFrame {
	
	private static final long serialVersionUID = 1L;
	private CameraData data = null;
	private List<Cluster> clusters = null;
	private FramePanel panel = null;
	
	public Viewer(CameraData data, List<Cluster> clusters, String imageFile, String imageFolder, int width, int height){
		super();
		this.clusters = clusters;
		this.data = data;
		
		Container container = this.getContentPane();
		
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		this.setSize(width, height);
		this.setResizable(false);
		
		this.setLocation(100, 100);
		this.setTitle("Bounding Box Viewer");
		
		Set<BoundingBox> boxes = new HashSet<BoundingBox>();
		List<Frame> frames = new Vector<Frame>();
		
		for (Frame f : data.getFrames()){
			for(Entity e : f.getEntities())
				boxes.add(e.getBoxes()[ExperimentManager.boxLocationId]);
			frames.add(f);
		}

		panel = new FramePanel(clusters, imageFile, imageFolder, width, height);
		
		container.add(panel);
		
		this.setVisible(true);
	}
	
	public String getCameraId(){
		return data.getCameraId();
	}
	
	public void setClusters(List<Cluster> clusters) {
		this.clusters = clusters;
	}

	public List<Cluster> getClusters() {
		return clusters;
	}
	
	public void setPanel(FramePanel panel) {
		this.panel = panel;
	}

	public FramePanel getPanel() {
		return panel;
	}
	
	/* ***************************************************
	 * CTOR ONLY FOR CLUSTERING TESTING!!!
	 ***************************************************** */
	@Deprecated
	public Viewer(Set<BoundingBox> boxes, List<Cluster> clusters){
		super();
		this.clusters = clusters;
//		this.data = data;
		
		Container container = this.getContentPane();
		
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		this.setSize(550, 377);
		this.setResizable(false);
		
		this.setLocation(100, 100);
		this.setTitle("Bounding Box Viewer");
		
		BoundingBoxPanel panel = new BoundingBoxPanel(boxes, clusters, true);
		BoundingBoxPlayerThread player = new BoundingBoxPlayerThread(panel);
		
		container.add(panel);
		player.start();
		
//		panel.repaint();
		
		this.setVisible(true);
	}
}
